package com.git.task;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.git.task.dao.TaskDaoImpl;

public class TaskServlet extends HttpServlet{

	/***/
	private static final long serialVersionUID = 1L;
	
	private TaskDaoImpl taskDao;
	
	public TaskServlet() {
		taskDao = new TaskDaoImpl();
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String method = req.getParameter("method");
		if("execute".equals(method)){
			
		}else if("saveTask".equals(method)){
			this.saveTask(req, resp);
			return;
		}else if("enterTask".equals(method)){
			this.enterTaskList(req, resp);
			return;
		}else if("viewChildTask".equals(method)){
			this.viewChildTask(req, resp);
			return;
		}else if("delTask".equals(method)){
			this.delChileTask(req, resp);
			return;
		}else if("upSort".equals(method)){
			this.updateTaskSort(req, resp);
			return;
		}

	}
	
	protected void updateTaskSort(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		if(null != req.getParameter("taskModelId")){
			long taskModelId = Long.valueOf(req.getParameter("taskModelId"));
			
			String[] taskIds = req.getParameterValues("taskId");
			
			for(String taskId : taskIds){
				int taskSort = Integer.valueOf(req.getParameter(taskId));
				taskDao.updateTaskSort(taskModelId, Integer.valueOf(taskId), taskSort);
			}
		}
		req.getRequestDispatcher("/TaskModelServlet?method=view").forward(req, resp);

	}
	
	protected void delChileTask(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		long taskModelId = Long.valueOf(req.getParameter("taskModelId"));
		int taskId = Integer.valueOf(req.getParameter("taskId"));
		taskDao.delChildTask(taskModelId, taskId);
		req.setAttribute("taskModelId", taskModelId);
//		req.getRequestDispatcher("/jsp/task/viewChildTaskInfo.jsp").forward(req, resp);
		this.viewChildTask(req, resp);
	}
	
	protected void enterTaskList(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
		IOException {
		TaskImpl task = new TaskImpl();
		if(null != req.getParameter("taskId") && !"".equals(req.getParameter("taskId"))){
			task.setTaskId(Integer.parseInt(req.getParameter("taskId").trim()));
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		if(null != req.getParameter("dateKey") && !"".equals(req.getParameter("dateKey"))){
			try {
				task.setDateKey(new java.sql.Date(format.parse(req.getParameter("dateKey")).getTime()));
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		List<TaskImpl> taskList = taskDao.getTaskList(task);
		req.setAttribute("taskList", taskList);
		req.setAttribute("taskModelId", req.getParameter("taskModelId"));
		req.getRequestDispatcher("/jsp/task/selectTaskInfo.jsp").forward(req, resp);
	}
	
	protected void executeTask(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		String taskId = req.getParameter("taskId");
		
		
	}
	
	protected void saveTask(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
		IOException {
		String childTask = req.getParameter("childTask");
		long taskModelId = Long.valueOf(req.getParameter("taskModelId"));
//		String dateKey = req.getParameter("dateKey");
//		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

		System.out.println(childTask);
		TaskImpl task = new TaskImpl();
		String[] tasks  = childTask.split(",");
		
		for(String taskid : tasks){
			task.setTaskId(Integer.parseInt(taskid));
			task.setTaskModelId(taskModelId);
			
//			try {
//				task.setDateKey(new java.sql.Date(format.parse(dateKey).getTime()));
//			} catch (ParseException e) {
//				e.printStackTrace();
//			}
			
			taskDao.saveTask(taskid, taskModelId);
		}

		req.setAttribute("taskModelId", taskModelId);
//		req.getRequestDispatcher("/TaskModelServlet?method=view").forward(req, resp);
		resp.sendRedirect("TaskModelServlet?method=view");
	}
	
	protected void viewChildTask(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
		IOException {
		long taskModelId = Long.valueOf(req.getParameter("taskModelId"));

		List<TaskImpl> taskList = taskDao.getTaskOrderByTaskId(taskModelId);
		
		req.setAttribute("taskList", taskList);
		req.setAttribute("taskModelId", req.getParameter("taskModelId"));

		req.getRequestDispatcher("/jsp/task/viewChildTaskInfo.jsp").forward(req, resp);

	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		doPost(req, resp);
	}
	
}
